<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
   <button onclick="sendMsg()">发送请求</button>
    <script>
        function sendMsg () {
            // 1.创建XMLhttpRequest对象
            var xhr = new XMLHttpRequest()
            // 2.调用open方法打开链接
            xhr.open('GET', 'data.php?id=1')
            // 3.发送send请求
            xhr.send()
            // 4.监听状态变化
            xhr.onreadystatechange = function () {
                // 判断状态值 0-4五种状态 4表示最终完成
                if (xhr.readyState === 4) {
                    // 判断状态码
                    if (xhr.status === 200) {
                        var res = JSON.parse(xhr.responseText)
                        console.log(res);
                    }
                }  

            }
        }
    </script>
</body>
</html>